package com.hardy.demo.hystrix;

import com.netflix.hystrix.*;

import java.util.concurrent.TimeUnit;

/**
 * Author: Hardy
 * Date:   2021/3/11
 * Description:
 **/
public class Action extends HystrixCommand {

    public Action(String name) {
        super(Setter
                // 分组配置
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey("Group"))

                // 命令配置
                .andCommandKey(HystrixCommandKey.Factory.asKey(name))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withExecutionTimeoutInMilliseconds(1000))

                // 线程池配置
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("ThreadPool"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(2)));
    }

    @Override
    protected String run() {
        System.out.println("hello HystrixCommand");
        return "hello HystrixCommand";
    }

    @Override
    protected String getFallback() {
        System.out.println("happen error");
        return "happen error";
    }
}
